/*
 * @Date: 2019-11-11 10:48:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-05-19 14:52:31
 * @describe: --
 */
import Vue from 'vue'
import Vuex from 'vuex'
import cloneDeep from 'lodash/cloneDeep'
import user from './modules/user'
import http from '@/utils/request'

Vue.use(Vuex)

export default new Vuex.Store({
    namespaced: true,
    state: {
        // 导航条, 布局风格, defalut(白色) / colorful(鲜艳)
        navbarLayoutType: 'colorful',
        // 侧边栏, 布局皮肤, default(白色) / dark(黑色)
        sidebarLayoutSkin: 'dark',
        // 侧边栏, 折叠状态
        sidebarFold: false,
        // 侧边栏, 菜单
        sidebarMenuList: [],
        sidebarMenuActiveName: '',
        // 内容, 是否需要刷新
        contentIsNeedRefresh: false,
        // 内容, 标签页(默认添加首页)
        contentTabs: [
            {
                ...window.SITE_CONFIG['contentTabDefault'],
                'name': 'home',
                'title': 'home'
            }
        ],
        contentTabsActiveName: 'home',
        ReceiveNum: 0,           //未读消息
        CityRegion: [],           //区域
        BusinessPage: [],        //商圈
        MetroPage: [],        //地铁线路
        maintainList: [],       //维护人
        DeptList: [],            //部门列表
        dictList: [],            //楼盘列表
    },
    modules: {
        user
    },
    mutations: {
        // 重置vuex本地储存状态
        resetStore(state) {
            Object.keys(state).forEach((key) => {
                state[key] = cloneDeep(window.SITE_CONFIG['storeState'][key])
            })
        },
        // val(state, opts) {
        //     state[opts.key] = opts.data;
        // }
    },
    actions: {
        lodAll({
            dispatch
        }) {
            let _this = this;
            dispatch('getCityRegion');
            dispatch('getBusinessPage');
            dispatch('getMetroPage');
            dispatch('getSimpleUserList');
            dispatch('getDeptList');
            dispatch('getDictList');
            dispatch('getReceiveNum');
            // this.commit('getReceiveNum');
            setInterval(function () {
                dispatch('getReceiveNum');
            }, 300 * 1000)
        },

        getCityRegion(store) {
            http.get(`/sys/region/city_region`)
                .then(({ data: res }) => {
                    if (res.code == 0) {
                        store.state.CityRegion = res.data;
                    }
                })
                .catch(() => { });
        },

        //获取本地商圈列表
        getBusinessPage(store) {
            http.get(`/dictionary/business/all`)
                .then(({ data: res }) => {
                    if (res.code == 0) {

                        store.state.BusinessPage = res.data;
                    }
                })
                .catch(() => { });
        },

        //获取本地地铁线路
        getMetroPage(store) {
            http.get(`/dictionary/metro/page`, {
                params: {
                    page: 1,
                    limit: 30
                }
            })
                .then(({ data: res }) => {
                    if (res.code == 0) {
                        store.state.MetroPage = res.data.list;
                    }
                })
                .catch(() => { });
        },
        //获取员工
        getSimpleUserList(store) {
            http.get(`/sys/user/simpleUserList`).then(({ data: res }) => {
                if (res.code == 0) {
                    store.state.maintainList = res.data
                }
            }).catch(() => { })
        },

        //获取部门列表
        getDeptList(store) {
            http.get(`/sys/dept/searchList`).then(({ data: res }) => {
                if (res.code == 0) {
                    let obj = res.data;
                    function getTreeData(data) {
                        // 循环遍历json数据
                        for (var i = 0; i < data.length; i++) {
                            if (data[i].children.length < 1) {
                                // children若为空数组，则将children设为undefined
                                data[i].children = undefined;
                            } else {
                                // children若不为空数组，则继续 递归调用本方法
                                getTreeData(data[i].children);
                            }
                        }
                        return data;
                    }
                    store.state.DeptList = getTreeData(obj)
                }
            }).catch(() => { })
        },
        //获取楼盘列表
        getDictList(store) {
            http.get(`/house/houseinfo/dictList`, {
                params: {
                    name: ""
                }
            })
                .then(({ data: res }) => {
                    store.state.dictList = res.data
                })
                .catch(() => { });
        },

        //获取未读消息数量
        getReceiveNum(store) {
            http.get("/sys/sysremind/receiveNum")
                .then(({ data: res }) => {
                    store.state.ReceiveNum = res.data;
                })
                .catch(() => { });
        },
    },
})
